﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microhard.Core.DBUtility;
using Microhard.Permission.Models;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microhard.Core;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace Microhard.Permission.Models
{
    public class DictionaryCache : ICacheOperator<DictionaryModel>
    {
        public void UpdateCache(string key, DictionaryModel newModel)
        {
            if (newModel == null)
                return;
            if (newModel.DataID == Guid.Empty)
                return;
            ICacheManager cm = CacheFactory.GetCacheManager("mhDic");

            cm.Add(newModel.Value.ToString(), newModel, CacheItemPriority.Normal, null, new NeverExpired());
        }

        public DictionaryModel GetFromCache(string dicID)
        {
            ICacheManager cm = CacheFactory.GetCacheManager("mhDic");
            return cm.GetData(dicID.ToString()) as DictionaryModel;
        }


        public void RemoveFormCache(List<string> keys)
        {
            ICacheManager cm = CacheFactory.GetCacheManager("mhDic");
            foreach (var key in keys)
            {
                cm.Remove(keys.ToString());
            }
        }

        public void RefreshCache()
        {
            ICacheManager cm = CacheFactory.GetCacheManager("mhDic");
            Microhard.Permission.Interface.IDictionary idic = IoC.Resolve<Microhard.Permission.Interface.IDictionary>();
            var dics = idic.Query(null);
            foreach (var d in dics)
            {
                cm.Add(d.Value.ToString(), d, CacheItemPriority.Normal, null, new NeverExpired());
            }
        }
    }
}




